package com.mall.seckill.schedule;

import com.mall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SechkillSkuSchedule {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;
    @Async
    @Scheduled(cron = "* * * * * *")
    public void uploadSeckillSku3Days(){
        RLock lock = redissonClient.getLock("seeckill:upload:lock");
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSku3Days();
        }catch (Exception e){
            lock.unlock();
        }
    }
}
